package com.hsteach.web.controller.system;

import java.util.List;
import java.util.Set;

import com.hsteach.common.core.domain.R;
import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.core.domain.model.WebLoginBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.hsteach.common.constant.Constants;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.domain.entity.SysMenu;
import com.hsteach.common.core.domain.entity.SysUser;
import com.hsteach.common.core.domain.model.LoginBody;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.framework.web.service.SysLoginService;
import com.hsteach.framework.web.service.SysPermissionService;
import com.hsteach.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(value = "登录相关", tags = "登录验证")
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
//        String token = loginService.login(loginBody.getAccessToken(), loginBody.getAppId(), loginBody.getToken(), loginBody.getOrgCode(), loginBody.getPersonId(), loginBody.getStudentId());
        String token = loginService.login(loginBody.getAccessToken(), loginBody.getAppId(), loginBody.getToken(), loginBody.getOrgCode(), loginBody.getPersonId(), loginBody.getStudentId(), loginBody.getSource(),loginBody.getMenuListSource());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * web登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/web/login")
    public AjaxResult webLogin(@RequestBody WebLoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.webLogin(loginBody.getOrgCode(), loginBody.getToken(), loginBody.getAccessToken(), loginBody.getAppId());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public R<AuthInfo> getInfo()
    {
        AuthInfo authInfo = SecurityUtils.getLoginUser().getAuthInfo();
        return R.ok(authInfo);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
